using EnoApi.Domain;
using EnoApi.Dto;
using EnoApi.Repositories;
using EnoApi.Tools;
using Microsoft.AspNetCore.Mvc;


namespace EnoApi.Controllers;

[ApiController]
[Route("api/blogs")]
public class BlogController : ControllerBase
{

    private readonly IReporsitory<Blogs> _reporsitory;

    public BlogController(IReporsitory<Blogs> reporsitory)
    {
        _reporsitory = reporsitory;
    }
    [HttpGet]
    public IActionResult GetAll()
    {
        var list = _reporsitory.GetAll();
        var tips = new Tips<List<Blogs>>(200, list, "查询所有博客成功");
        return Ok(tips);
    }
    [HttpPost]
    public IActionResult AddBlog(BlogsDto blogDto)
    {
        var blog = new Blogs { Title = blogDto.Title, Content = blogDto.Content, PublishName = blogDto.PublishName };
        var data = _reporsitory.AddBlog(blog);
        var tips = new Tips<Blogs>(200, data, "新增博客成功");
        return Ok(tips);
    }

    [HttpDelete("{id}")]
    public IActionResult DelBlog(int id)
    {
        var blog = _reporsitory.GetById(id);
        var tips = new Tips<string>(200, "", "删除的博客成功");
        if (blog == null)
        {
            var tips1 = new Tips<string>(300, "", "删除的博客不存在");
            return Ok(tips1);
        }
        _reporsitory.DelBlog(blog);
        return Ok(tips);
    }

    [HttpPut("{id}")]
    public IActionResult UpdateBlog(int id, BlogsDto blogsDto)
    {
        var blog = _reporsitory.GetById(id);
        var tips = new Tips<string>(200, "", "修改博客成功");
        if (blog == null)
        {
            var tips1 = new Tips<string>(300, "", "修改的博客不存在");
            return Ok(tips1);
        }

        blog.Title = blogsDto.Title;
        blog.Content = blogsDto.Content;
        blog.PublishName = blogsDto.PublishName;

        _reporsitory.UpdBlog(blog);
        return Ok(tips);
    }

}